import { DateHelper, ViewPreset } from 'major-schedulerpro'
/**
 * monthAndYear 时间换算
 * @param {*} startDate
 * @param {*} diffDate  与北京时间时差（小时）
 * @param {*} daetFormat 格式化
 */
function getMonthAndYear(startDate, diffDate, dateFormat = 'M月D HH:mm') {
  return DateHelper.format(new Date(startDate), dateFormat)
}

export let standardTime = +new Date()
/**
 * 自定义小时（T） 时间轴
 * @param {*} standardTime T时刻
 * @returns
 */
export function getHourViewPresetData(suffix = '') {
  const viewPreIncrement = [1, 2],
    viewPresetConfig = []
  for (let index = 0; index < viewPreIncrement.length; index++) {
    const incrementValue = viewPreIncrement[index],
      presetsId = 'current' + index + suffix
    viewPresetConfig.push(
      new ViewPreset({
        id: presetsId,
        name: presetsId,
        sortTime: incrementValue * 60 * 60 * 1000,
        label: incrementValue + '小时',
        tickWidth: 100,
        tickHeight: 60,
        shiftIncrement: 60,
        shiftUnit: 'second',
        defaultSpan: 60,
        timeResolution: {
          unit: 'second',
          increment: 1,
        },
        headers: [
          // {
          //   unit: 'day',
          //   dateFormat: 'ddd DD/MM', //Mon 01/10
          // },
          {
            unit: 'hour',
            increment: incrementValue, // 一个单元格代表小时
            renderer: (start) => {
              // 处理时间
              const TTime = Math.round(
                  (+new Date(start) - standardTime) / 1000 / 60 / 60
                ),
                styles = TTime === 0 ? 'major-gantt-T' : ''

              return '<label class="' + styles + '">T' + TTime + '小时</label>'
            },
          },
        ],
      })
    )
  }
  return viewPresetConfig
}

/**
 * 获取秒级 viewPreset
 * @returns
 */
export function getSecondViewPresetData() {
  const viewPreIncrement = [1, 2, 5, 10, 15, 30]

  let viewPresetConfig = []
  for (let index = 0; index < viewPreIncrement.length; index++) {
    const viewPresrtValue = viewPreIncrement[index]
    viewPresetConfig.push(
      new ViewPreset({
        id: 'second' + viewPresrtValue,
        name: 'second' + viewPresrtValue,
        tickWidth: 100,
        tickHeight: 60,
        shiftIncrement: 60,
        sortTime: viewPresrtValue * 1000,
        timeType: '秒',
        timeSize: 1000,
        label: viewPresrtValue === 90 ? '1.5分' : viewPresrtValue + '秒',
        shiftUnit: 'second',
        defaultSpan: 60,
        timeResolution: {
          unit: 'second',
          increment: 1,
        },
        headers: [
          /*  {
            unit: 'minute',
            dateFormat: 'YYYY年M月',
            increment: Infinity,
            renderer: start => DateHelper.format(start, 'YYYY年MM月D日')
          }, */
          {
            unit: 'second',
            increment: viewPresrtValue,
            renderer: (start) => {
              const T = (+new Date(start) - standardTime) / 1000
              return T < 0 ? '' : T + '秒'
            },
          },
        ],
      })
    )
  }
  return viewPresetConfig
}

/**
 * 天
 * @returns
 */
export function getDayViewPresetData() {
  let viewPresetConfig = []
  const data = [
    {
      id: 'dayAndMonth',
      name: 'DayAndMonth',
      tickWidth: 60,
      tickHeight: 105,
      label: '天月',
      sortTime: 30 * 24 * 60 * 60 * 1000,
      displayDateFormat: 'll',
      shiftUnit: 'day',
      shiftIncrement: 1,
      defaultSpan: 4,
      timeResolution: {
        unit: 'hour',
        increment: 1,
      },
      headers: [
        // {
        //   unit: 'month',
        //   dateFormat: 'YYYY年M月',
        //   increment: Infinity,
        //   renderer: (start) =>
        //     `<div id='headerCalendar' class='header-calendar-right'>${DateHelper.format(
        //       start,
        //       'YYYY年MM月'
        //     )}</div>`,
        // },

        {
          unit: 'month',
          dateFormat: 'YYYY年M月',
          // renderer: (start) => getMonthAndYear(start, 0, 'D日'),
        },
        {
          unit: 'day',
          increment: 1,
          renderer: (start) => getMonthAndYear(start, 0, 'D日'),
        },
      ],
    },
    {
      id: 'weekDateAndMonth',
      name: 'Weeks 3',
      tickWidth: 90,
      label: '天周',
      sortTime: 7 * 24 * 60 * 60 * 1000,
      shiftIncrement: 1,
      defaultSpan: 7,
      displayDateFormat: 'll',
      shiftUnit: 'week',
      timeResolution: {
        unit: 'day',
        increment: 1,
      },
      headers: [
        {
          unit: 'year',
          // dateFormat: 'YYYY年',
          increment: Infinity,
          dateFormat: 'YYYY年MM月',
        },

        {
          unit: 'week',
          renderer: (start) => getMonthAndYear(start, 0, 'D日（dd）'),
        },
        {
          unit: 'week',
          increment: 1,
          renderer: () => '',
        },
      ],
    },
    {
      id: 'monthAndYear',
      name: 'Months',
      tickWidth: 60, //宽度根据最后一个时间单位 增量确定
      displayDateFormat: 'll',
      shiftIncrement: 1,
      shiftUnit: 'month',
      defaultSpan: 1,
      label: '年月',
      sortTime: 365 * 24 * 60 * 60 * 1000,
      timeResolution: {
        unit: 'day',
        increment: 1,
      },
      headers: [
        {
          unit: 'year',
          dateFormat: 'YYYY年',
          htmlencode: false,
          increment: Infinity,
          renderer: (start) =>
            `<div id='headerCalendar' class='header-calendar-right'>${DateHelper.format(
              start,
              'YYYY年MM月'
            )}</div>`,
        },
        {
          unit: 'month',
          renderer: (start) => getMonthAndYear(start, 13, 'M月D日'),
        },
        {
          unit: 'month',
          increment: 1,
          renderer: () => '',
        },
      ],
    },
    {
      id: 'weekAndDayLetter',
      name: 'Weeks 2',
      label: '周天',
      sortTime: 7 * 24 * 60 * 60 * 1000,
      tickWidth: 240,
      shiftIncrement: 1,
      defaultSpan: 12,
      displayDateFormat: 'll',
      shiftUnit: 'week',
      timeResolution: {
        unit: 'day',
        increment: 1,
      },
      headers: [
        {
          unit: 'year',
          dateFormat: 'YYYY年',
          increment: Infinity,
          renderer: (start) => DateHelper.format(start, 'YYYY年MM月'),
        },
        {
          unit: 'day',
          increment: 1,
          renderer: (start) => DateHelper.format(start, 'MM月D日'),
        },
        {
          unit: 'day',
          increment: 1,
          renderer: () => '',
        },
      ],
    },
  ]

  for (const item of data) {
    viewPresetConfig.push(new ViewPreset(item))
  }
  return viewPresetConfig
}
/**
 * 获取分钟 viewPreset
 * @returns
 */
export default function getMinuteViewPresetData() {
  // const viewPreIncrement = [1, 5, 10, 15, 30] //, 90
  const viewPreIncrement = [10] //, 90
  let viewPresetConfig = []
  for (let index = 0; index < viewPreIncrement.length; index++) {
    const incrementValue = viewPreIncrement[index]
    viewPresetConfig.push(
      new ViewPreset({
        id: 'minute' + incrementValue,
        name: 'minute' + incrementValue,
        tickWidth: 100,
        tickHeight: 60,
        shiftIncrement: 60,
        shiftUnit: 'second',
        defaultSpan: 60,
        sortTime: incrementValue * 60 * 1000,
        timeType: '分',
        timeSize: 60000,

        label: incrementValue === 60 ? '1小时' : incrementValue + '分',
        timeResolution: {
          unit: 'second',
          increment: 1,
        },
        headers: [
          {
            unit: 'minute',
            increment: incrementValue, //一个单元格代表分钟数
            renderer: (start) => {
              const T = (+new Date(start) - standardTime) / 60000 / 10
              return T < 0 ? '' : 'T' + T
            },
          },
        ],
      })
    )
  }
  return viewPresetConfig
}
